﻿/*
 * ========================================================================
 * Copyright(c) 2007-2012 情留メ蚊子, All Rights Reserved.
 * Welcom use the LiteFetion.
 * See more information,Please goto http://www.94qing.com
 * ========================================================================
 * 
 * 作 者：情留メ蚊子
 * Q  Q: 540644769
 * 邮 箱：qlwz@qq.com
 * 网 址：http://www.94qing.com
 * ========================================================================
*/
using System;
using System.Net;
using System.Threading;
using LiteFetion.Model;
using LiteFetion.Notify;
using LiteFetion.Utils;
using Newtonsoft.Json.Linq;

namespace LiteFetion
{
    /// <summary>
    /// 通知管理
    /// </summary>
    public class NotifyManager
    {
        public NotifyManager(FetionClient client)
        {
            FetionClient = client;
        }

        /// <summary>
        /// FetionClient
        /// </summary>
        public FetionClient FetionClient { get; private set; }

        /// <summary>
        /// User
        /// </summary>
        public User User { get { return FetionClient.User; } }

        public ActionResult PollNotify()
        {
            string postData = string.Format("ssid={0}", this.User.SessionId);
            HttpHelper http = new HttpHelper();
            http.Url = Gobal.WEBIM_URL_CONNECT.FormatWith(this.FetionClient.RequestVersion++);
            http.Method = Method.POST;
            http.PostData = postData;
            http.Cookies = this.User.Cookies;
            http.Do();

            this.FetionClient.Debug(http);

            if (http.StatusCode != HttpStatusCode.OK)
            {
                if (this.FetionClient.PollNotifyFailed <= Gobal.FETION_MAX_POLL_NOTIFY_FAILED)
                {
                    this.FetionClient.PollNotifyFailed++;
                    Thread.Sleep(1000);
                    return ActionResult.SUCCESS;
                }
                else
                {
                    return ActionResult.HTTP_FAILED;
                }
            }
            JObject json = Util.GetObjectResult(http.Html);
            if (json == null)
            {
                return ActionResult.SUCCESS;
            }
            int status = json.Value<int>("rc");
            if (status == 310)
            {
                new ClientStateNotify(this.FetionClient, ClientState.NET_ERROR);
                return ActionResult.HTTP_FAILED;
            }
            else if (status != 200)
            {
                Thread.Sleep(1000);
                return ActionResult.SUCCESS;
            }

            JArray jArrays = json.Value<JArray>("rv");
            if (jArrays == null)
            {
                jArrays = new JArray();
            }
            JObject temp = null;
            for (int j = 0; j < jArrays.Count; j++)
            {
                temp = Util.GetObjectResult(jArrays[j].ToString());
                this.ProcessNotify(temp);
            }
            return ActionResult.SUCCESS;
        }

        private void ProcessNotify(JObject json)
        {
            int dataType = json.Value<int>("DataType");
            JObject data = json.Value<JObject>("Data");
            switch (dataType)
            {
                case 1:
                    //自己的信息
                    //sv=积分 lv=等级  lvs=升级进度
                    break;
                case 2://好友状态信息
                    new BuddyStateNotify(this.FetionClient, data);
                    break;
                case 3:	//好友消息
                    new BuddyMessageNotify(this.FetionClient, data);
                    break;
                case 4:	//退出信息
                    new ClientStateNotify(this.FetionClient, data);
                    break;
                case 5:	//添加好友请求
                    new BuddyInviteNotify(this.FetionClient, data);
                    break;
                case 6:	//添加好友的回复,会发三个通知，这里只处理ba=1的那个
                    if (data.Value<int>("ba") == 1)
                    {
                        new BuddyConfirmedNotify(this.FetionClient, data);
                    }
                    //if (data.Value<int>("ba") == 1)  //被删除好友
                    break;
            }
        }
    }
}
